TelescopeMapArgs = TelescopeMapArgs or {}

-- nnoremap <Leader>f :lua require'telescope.builtin'.find_files(require('telescope.themes').get_dropdown({}))<cr>

local map_tele = function(key, f, options, buffer)
	local map_key = vim.api.nvim_replace_termcodes(key .. f, true, true, true)

	TelescopeMapArgs[map_key] = options or {}

	local mode = "n"
	local rhs = string.format("<cmd>lua require'telescope.builtin'['%s'](TelescopeMapArgs['%s'])<CR>", f, map_key)

	local map_options = {
		noremap = true,
		silent = true,
	}

	if not buffer then
		vim.api.nvim_set_keymap(mode, key, rhs, map_options)
	else
		vim.api.nvim_buf_set_keymap(0, mode, key, rhs, map_options)
	end
end
local ivy = {
  theme = "ivy"
}
local dropdown= {
  theme = "dropdown"
}
local command_palette = require("modules.tool.command_palette")
require("telescope").setup({
  pickers = {
    -- find_files =  dropdown,
    --
    live_grep = {
      additional_args = function(opts)
        return {"--hidden"}
      end
    },
    lsp_references = ivy,
    buffers = dropdown,
    oldfiles =   ivy,
    lsp_document_symbols = ivy ,
    -- lsp_dynamic_workspace_symbols = ivy ,
    treesitter = ivy
  },
	defaults = {
    vimgrep_arguments = {
      'rg',
      '--color=never',
      '--no-heading',
      '--with-filename',
      '--line-number',
      '--column',
      '--smart-case',
      '-u' -- thats the new thing
    },
		prompt_prefix = "🔭 ",
		selection_caret = " ",
		layout_config = {
			horizontal = { prompt_position = "bottom", results_width = 0.6 },
		},
		file_previewer = require("telescope.previewers").vim_buffer_cat.new,
		grep_previewer = require("telescope.previewers").vim_buffer_vimgrep.new,
		qflist_previewer = require("telescope.previewers").vim_buffer_qflist.new,
		file_sorter = require("telescope.sorters").get_fuzzy_file,
		file_ignore_patterns = {},
		generic_sorter = require("telescope.sorters").get_generic_fuzzy_sorter,
		-- path_display = { "absolute" },
		dynamic_preview_title = true,
		winblend = 0,
		border = {},
		borderchars = {
			"─",
			"│",
			"─",
			"│",
			"╭",
			"╮",
			"╯",
			"╰",
		},
		color_devicons = true,
		use_less = true,
		set_env = { ["COLORTERM"] = "truecolor" },
	},
	extensions = {
    command_palette = command_palette,
		ctags_outline = {
			--ctags option
			ctags = { "ctags" },
			--ctags filetype option
			ft_opt = {
				vim = "--vim-kinds=fk",
				lua = "--lua-kinds=fk",
			},
		},
		fzf = {
			fuzzy = true, -- false will only do exact matching
			override_generic_sorter = true, -- override the generic sorter
			override_file_sorter = true, -- override the file sorter
			case_mode = "smart_case", -- or "ignore_case" or "respect_case"
			-- the default case_mode is "smart_case"
		},
		--[[fzy_native = {
      fuzzy = false,
			override_generic_sorter = false,
			override_file_sorter = true,
			case_mode = "smart_case", -- or "ignore_case" or "respect_case"
		},]]
		media_files = {
			-- filetypes whitelist
			-- defaults to {"png", "jpg", "mp4", "webm", "pdf"}
			filetypes = { "png", "webp", "jpg", "jpeg" },
			find_cmd = "rg", -- find command (defaults to `fd`)
		},
    neoclip={},
    aerial={},
		dap = {},
    refactoring = {},
    live_grep_args = {
	    layout_strategy = "vertical",
    },
		frecency = {
			show_scores = true,
			show_unindexed = true,
			ignore_patterns = { "*.git/*", "*/tmp/*" },
		},
	},
})
require('neoclip').setup()
require("telescope").load_extension("fzf")
--require("telescope").load_extension("frecency")
if vim.g.use_dap==1 then
  require("telescope").load_extension("dap")
  require("telescope").load_extension("i23")
end
require("telescope").load_extension("refactoring")
require('telescope').load_extension('aerial')
require('telescope').load_extension('neoclip')
require('telescope').load_extension('command_palette')
require("telescope").load_extension("live_grep_args")
--require("telescope").load_extension("ctags_outline")

vim.api.nvim_set_keymap(
	"v",
	"<leader>rr",
	"<Esc><cmd>lua require('telescope').extensions.refactoring.refactors()<CR>",
	{ noremap = true }
)
local sorters = require("telescope.sorters")
map_tele("cw", "quickfix", {
	layout_strategy = "vertical",
})

map_tele("<leader>ce", "lsp_document_diagnostics", {
	layout_strategy = "vertical",
})
map_tele("<space>fq", "quickfix", {
	layout_strategy = "vertical",
	sorting_strategy = "ascending",
})
map_tele("<space>fh", "oldfiles", {})
map_tele("<space>gs", "git_status", {})
map_tele("<space>hh", "oldfiles", {
	-- layout_strategy = "vertical",
	-- sorting_strategy = "ascending",
})
map_tele("<space>rs", "treesitter", {
	-- layout_strategy = "vertical",
	-- sorting_strategy = "ascending",
})
-- map_tele("<space>o", "lsp_document_symbols", {
--   sorting_strategy = "ascending",
-- })
--map_tele("<space>ca", "lsp_code_actions", {})
--map_tele("<space>qf", "lsp_code_actions", {})
map_tele("<space>cr", "lsp_range_code_actions", {})
-- map_tele("<space>bb", "buffers", {})

map_tele("gr", "lsp_references", {
	-- layout_strategy = "vertical",
	-- short_path = true,
})
map_tele("<space>fw", "grep_string", {
	short_path = true,
	word_match = "-w",
	only_sort_text = true,
	layout_strategy = "vertical",
	sorter = sorters.get_fzy_sorter(),
})
map_tele("<C-P>", "find_files",{
	layout_strategy = "horizontal",
  layout_config ={
		width = 0.95,
		results_width= 0.9,
  }
})
map_tele("<C-S>", "live_grep",{
	layout_strategy = "vertical"
})
map_tele("<Leader>fa", "live_grep",{
	layout_strategy = "vertical"
})
map_tele("<Leader>ws", "lsp_dynamic_workspace_symbols",{
	short_path = true,
	layout_strategy = "vertical"
})
map_tele("<Leader>wt", "treesitter")
map_tele("<Leader>bb", "buffers")
map_tele("<Leader>ss", "lsp_dynamic_workspace_symbols",{
	short_path = true,
	layout_strategy = "vertical"
	-- layout_strategy = "vertical"
})
-- require("telescope").extensions.media_files.media_files()
-- require("telescope")load_extension("project")
